package com.gxuwz.medical.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.institution.Institution;
import com.gxuwz.medical.vo.PageBean;

public class InstitutionDao extends GenericDao<Institution>{
	@Override
	protected Institution handle(ResultSet rs) throws SQLException {	
		Institution i =new Institution();
		i.setAreacode(rs.getString("areacode"));
		i.setAgencode(rs.getString("agencode"));
		i.setAgenname(rs.getString("agenname"));
		i.setGrade(rs.getInt("grade"));
		return i;
	}
	public void add(Institution i) throws SQLException {
		try {
			DbUtil db = new DbUtil();
			String sql = "insert into institution(agencode,agenname,areacode,grade) values('"+i.getAgencode()+"','"+i.getAgenname()+"','"+i.getAreacode()+"',"+i.getGrade()+")";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("删除农合机构失败" + e.getMessage(), e);
		}
	}
	public void del(String agencode) throws SQLException {
		try {
			DbUtil db = new DbUtil();
			String sql = "delete from institution where agencode='" + agencode + "'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("删除农合机构失败" + e.getMessage(), e);
		}
	}
	public void upadte(Institution i) throws SQLException {
		try {
			DbUtil db = new DbUtil();
			String sql = "update institution set areacode='"+i.getAreacode()+"',agenname='"+i.getAgenname()+"',grade="+i.getGrade()+" where agencode='"+i.getAgencode()+"'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("删除农合机构失败" + e.getMessage(), e);
		}
	}
	public Institution query(String agencode) throws SQLException {
		String sql ="select * from institution where agencode = ?";
		Object[] params={agencode};
		List<Institution> list = new ArrayList<Institution>();
		list = super.queryAll(sql, params);
		if(list.size()!=0)
			return list.get(0);
		else
			return null;
	}
	public List<Institution> queryAll() throws SQLException {
		String sql ="select * from institution";
		return super.queryAll(sql, null);
	}
	public PageBean queryAll(int page,int size) throws SQLException {
		String sql ="select * from institution";
		return super.queryAll(sql, null,page,size);
	}
	
}
